<template>
  <div class="servicePackageList padding_20">
    <el-input v-model="searchCondition"
              placeholder="请输入病种名称"
              class="w-200"
              clearable
              @clear="handleQuery" />
    <el-button type="primary"
               @click="handleQuery">查 询</el-button>
    <el-button type="primary"
               @click="showEdit(null)">新 增</el-button>
    <!--病种列表-->
    <Table stripe
           border
           :columns="titles"
           :data="dataList"
           class="mt-20">
      <template slot-scope="{row,index}"
                slot="action">
        <span @click="showEdit(index)">编辑</span>
        <span @click="deleteDisease(index)">删除</span>
      </template>
    </Table>
    <!--分页-->
    <Page :current.sync="currentPage"
          :total="totals"
          :page-size="pageSize"
          @on-change="initTableData"
          show-total />

    <Modal v-model="editModel"
           title="编辑病种"
           :loading="loading"
           @on-ok="save">
      <div>
        病种名称： <Input v-model="editObject.name"
               placeholder="请输入病种名称"
               style="width: 300px" />
      </div>
      <div style="margin-top: 20px">
        病种介绍： <Input v-model="editObject.introduceFileUrl"
               enter-button="上传"
               style="width: 300px"
               placeholder="请输入病种介绍文件地址" />
      </div>
      <div style="margin-top: 20px">
        上传文件：
        <Upload :action="fileUploadAction"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
                accept=".pdf"
                :format="fileType">
          <Button icon="ios-cloud-upload-outline">上传</Button>
        </Upload>
        (仅支持PDF文件)
      </div>
    </Modal>

    <Modal v-model="confirmModal"
           title="是否删除"
           :loading="loading"
           @on-ok="goToDelete"
           @on-cancel="cancelDelete">
      <p>您确定删除该条数据吗？</p>
    </Modal>
  </div>
</template>

<script>
  export default {
    name: "referralDisease",
    data () {
      let vm = this;
      return {
        searchCondition: null,
        currentPage: 1,
        totals: 0,
        pageSize: 20,
        loading: false,
        confirmModal: false,
        editModel: false,
        fileUploadAction: this.api.file.upload,
        fileType: ["*.pdf"],
        titles: [
          {
            title: '病种名称',
            key: 'name'
          },
          {
            title: '转诊病种介绍',
            key: 'introduceFileUrl'
          },
          {
            title: '创建时间',
            key: 'createTime',
            render: (h, params) => {
              return h('span',
                this.commonFunction.timestampFormat(new Date(params.row.createTime), 'yyyy-MM-dd hh:mm:ss'));
            }
          },
          {
            title: '修改时间',
            key: 'laseUpdateTime',
            render: (h, params) => {
              return h('span',
                this.commonFunction.timestampFormat(new Date(params.row.lastUpdateTime), 'yyyy-MM-dd hh:mm:ss'));
            }
          },
          {
            title: '操作',
            slot: 'action',
            align: 'center',
            width: 100
          }
        ],
        dataList: [],
        editObject: {
          name: "",
          introduceFileUrl: ""
        }
      }
    },
    created () {
      this.initTableData();
    },
    methods: {
      handleQuery() {
        this.currentPage = 1
        this.initTableData()
      },
      initTableData () {
        let param = {
          "searchCondition": this.searchCondition,
          "pageIndex": this.currentPage - 1,
          "pageSize": this.pageSize
        };
        this.post(this.api.referralDisease.getReferralDiseaseList, param).then((res) => {
          if (res.data.code === '200') {
            this.dataList = res.data.object;
            this.totals = res.data.totalNum;
          } else {
            this.$Message.error(res.data.message);
          }
        });
      },
      handleChange (page) {
        let _start = (page - 1) * this.pageSize;
        let _end = page * this.pageSize;
      },
      showEdit (index) {
        this.editModel = true;
        if (index != null && index != undefined) {
          this.editObject = this.dataList[index];
        } else {
          this.editObject = {
            name: "",
            introduceFileUrl: ""
          }
        }
      },
      deleteDisease (index) {
        if (index != null && index != undefined) {
          this.editObject = this.dataList[index];
          this.confirmModal = true;

        } else {
          this.$Message.error("请选择需要删除数据");
        }
      },
      goToDelete () {
        this.post(this.api.referralDisease.deleteDisease, this.editObject).then((res) => {
          if (res.data.code === '200') {
            this.initTableData();
          } else {
            this.$Message.error(res.data.message);
          }
        });
      },
      cancelDelete () {
        this.editObject = {};
      },
      save () {
        if (!this.editObject.name) {
          this.$Message.error("病种名称不能为空");
          return;
        }
        this.post(this.api.referralDisease.uploadInfo, this.editObject).then((res) => {
          if (res.data.code === '200') {
            this.initTableData();
          } else {
            this.$Message.error(res.data.message);
          }
        });
      },
      handleAvatarSuccess (res) {
        this.editObject.introduceFileUrl = res.object[0];
      },
      beforeAvatarUpload (file) {
        const isLt2M = file.size / 1024 / 1024 < 10;
        if (!isLt2M) {
          this.$message.error('上传文件大小不能超过10MB!');
        }
        return isLt2M;
      },
    }
  }
</script>

<style scoped>
</style>
